package com.web.security.handler;

import com.common.constant.RedisConstant;
import com.common.utils.JwtTokenUtil;
import com.common.utils.RedisUtil;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author liuxuhuang
 * @date 2023年04月02日 10:42
 */
@Component
public class LogoutHandler implements org.springframework.security.web.authentication.logout.LogoutHandler {
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @Override
    public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        String token = jwtTokenUtil.getToken(request);
        long userId = jwtTokenUtil.getUserId(token);
        String redisKey = RedisConstant.LOGIN_USER_PREFIX + userId;
        redisUtil.del(redisKey);
        SecurityContextHolder.clearContext();
    }
}
